Type.registerNamespace("Insp");
Type.registerNamespace("Insp.UI");

Insp.UI.Widget = function(element)
{
    Insp.UI.Widget.initializeBase(this,[element]);
    this._url="";
    this._jsPrototype="";
    
    this._callbackDelegate = null;
    this._errorDelegate = null;
    this._jsExecDelegate = null;
}

Insp.UI.Widget.prototype = {
    get_url: function(){return this._url;},
    set_url: function(value){this._url = value;this.raisePropertyChanged('url');},
    
    get_jsPrototype: function(){return this._jsPrototype;},
    set_jsPrototype: function(value){this._jsPrototype = value;this.raisePropertyChanged('jsPrototype');},

    dispose: function()
    {
        if (this._callbackDelegate)delete this._callbackDelegate;
        if (this._errorDelegate)delete this._errorDelegate;
        if (this._jsExecDelegate)delete this._jsExecDelegate;
		Insp.UI.Widget.callBaseMethod(this,'dispose');
    },
    initialize: function()
    {
        Insp.UI.Widget.callBaseMethod(this,'initialize');
		this.addCssClass("ptModWidgt");
		this.addCssClass("moduleloading");
	
    	if (this._callbackDelegate === null) {
            this._callbackDelegate = Function.createDelegate(this, this.callbackHandler);
        }
        if (this._errorDelegate === null) {
            this._errorDelegate = Function.createDelegate(this, this._errorHandler);
        }
    	if (this._jsExecDelegate === null) {
            this._jsExecDelegate = Function.createDelegate(this, this._jsExecHandler);
        }
    },
    updated: function()
    {
        Portal.WidgetSvc.a(this.get_url(),this._callbackDelegate);
        Insp.UI.Widget.callBaseMethod(this,'updated');
    },
    callbackHandler: function(results)
    {
		if(results !== '')
		{
			this.removeCssClass("moduleloading");
			this.get_element().innerHTML = results;
			var d = this.get_element().getElementsByTagName("script");
			var t=d.length;
			for (var i=0;i<t;i++){
				var newScript = document.createElement('script');
				newScript.type = "text/javascript";
				newScript.text = d[i].text;
				if (d[i].src != '')
					newScript.src = d[i].src;
				this.get_element().appendChild (newScript);
			}
	        
			if(this.get_jsPrototype().length > 0)
			{
				window.setTimeout(this._jsExecDelegate,1000);
			}
		}else
			this._errorHandler();
    },
    _errorHandler: function(results)
    {
		this.removeCssClass("moduleloading");
		this.get_element().innerHTML = "<div style='margin:0;padding:1em 0;text-align:center;' class='feedErrorMessage'>"+widgetErrStr+"</div>";
    },
    _jsExecHandler: function()
    {
        obj = eval(this.get_jsPrototype());
        if(obj)obj.Execute(this.get_element());
    }
}
Insp.UI.Widget.registerClass('Insp.UI.Widget',Insp.UI.ModuleContentCtl);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
Type.registerNamespace("Insp");
Type.registerNamespace("Insp.UI");
var currModule = null;

Insp.UI.TelusWeather = function(element)
{
    Insp.UI.TelusWeather.initializeBase(this,[element]);
    this._cityCode = "";
    this._tempType = "";
    this._cobrand = "";        
    this._moduleClientID =  null;
}

Insp.UI.TelusWeather.prototype = {
    get_cityCode: function(){return this._cityCode;},
    set_cityCode: function(value){this._cityCode = value;this.raisePropertyChanged('cityCode');},
    
    get_tempType: function(){return this._tempType;},
    set_tempType: function(value){this._tempType = value;this.raisePropertyChanged('tempType');},

    get_cobrand: function(){return this._cobrand;},
    set_cobrand: function(value){this._cobrand = value;this.raisePropertyChanged('cobrand');},

    dispose: function()
    {
        Insp.UI.TelusWeather.callBaseMethod(this,'dispose');
    },
    initialize: function()
    {
        Insp.UI.TelusWeather.callBaseMethod(this,'initialize');
		this.addCssClass("ptModWeather");
		this.addCssClass("moduleloading");
    },
    updated: function()
    {
        this._createComponents();
        Insp.UI.TelusWeather.callBaseMethod(this,'updated');
    },
    _createComponents: function()
    {        
        //Hardcoding the url's here becoz there is no point in storing these in profiles. Moreover, they are still present
        //in a single place
        if(this.get_cobrand() == "en")
        {
            url = "http://btn.weather.ca";
            width = '290px';
            padding_left = '10px';
        }
        else
        {
            url = "http://btn.meteomedia.ca";
            width = '298px';
            padding_left = '6px';
        }
        this.get_element().innerHTML = "<iframe runat='server' src='"+ url +"/weatherbuttons/wxinfo.php?placeCode="+ this.get_cityCode()+"&celsiusF="+this.get_tempType()+"&clientID=telus'"
                                       +"height='370px' width='"+width+"' frameborder='0' scrolling='no' style='padding-left:"+padding_left+"'></iframe>";

    }
}

Insp.UI.TelusWeather.registerClass('Insp.UI.TelusWeather',Insp.UI.ModuleContentCtl,Sys.IContainer);

Insp.UI.TelusWeatherEdit = function(element)
{
    Insp.UI.TelusWeatherEdit.initializeBase(this,[element]);
    this._city = "";
    this._tempType = "";
    this._cityCode = "";
    this._saveBtnID = null;
    this._cancelBtnID = null;
    this._updatePanelID = null;
    this._moduleClientID = null;
    this._errorMessage1 = strTelusWeather_Err1;
    this._errorMessage2 = strTelusWeather_Err2;
    this._errorMessage3 = strTelusWeather_Err3;
    this._mulMessage1 = strTelusWeather_MulMessage1;
    this._mulMessage2 = strTelusWeather_MulMessage2;
    this._wError = false;
    this._multipleLoc = false;
    this._criteriaChanged = false;
    
    this._saveClickDelegate = null;
    this._cancelClickDelegate = null;
    this._criteriaChangeDelegate = null;
    this._saveCallbackSuccessDelegate = null;
    this._saveCallbackFailureDelegate = null;        
    this._LocationsDelegate = null;
    this._EnterKeyPressedDelegate = null;
}

Insp.UI.TelusWeatherEdit.prototype = {
    get_city: function(){return this._city;},
    set_city: function(value){this._city = value;this.raisePropertyChanged('city');},
    
    get_cityCode: function(){return this._cityCode;},
    set_cityCode: function(value){this._cityCode = value;this.raisePropertyChanged('cityCode');},
    
    get_tempType: function(){return this._tempType;},
    set_tempType: function(value){this._tempType = value;this.raisePropertyChanged('tempType');},

    get_moduleClientID: function(){return this._moduleClientID;},
    set_moduleClientID: function(value){this._moduleClientID = value;this.raisePropertyChanged('moduleClientID');},
    
    dispose: function()
    {
                
        if(this._saveCallbackFailureDelegate) delete this._saveCallbackFailureDelegate;
        if(this._saveCallbackSuccessDelegate) delete this._saveCallbackSuccessDelegate;
        
        $removeHandler($get('BtnCancel',this.get_element()),'click',this._cancelClickDelegate);
        if(this._cancelClickDelegate) delete this._cancelClickDelegate;
        
        $removeHandler($get('BtnSave',this.get_element()),'click',this._saveClickDelegate);
        if(this._saveClickDelegate) delete this._saveClickDelegate;
        
        $removeHandler($get('criteria',this.get_element()),'keypress',this._criteriaChangeDelegate);
        if(this._criteriaChangeDelegate) delete this._criteriaChangeDelegate;

        if(this._LocationsDelegate) delete this._LocationsDelegate;
        if(this._EnterKeyPressedDelegate) delete this._EnterKeyPressedDelegate;
        
        Insp.UI.TelusWeatherEdit.callBaseMethod(this,'dispose');
    },
    initialize: function()
    {
        Insp.UI.TelusWeatherEdit.callBaseMethod(this,'initialize');
        if (this._saveClickDelegate === null) {
            this._saveClickDelegate = Function.createDelegate(this, this._saveClickHandler);
        }
        $addHandler($get('BtnSave',this.get_element()),'click',this._saveClickDelegate);

        if (this._criteriaChangeDelegate === null) {
            this._criteriaChangeDelegate = Function.createDelegate(this, this._criteriaChangeHandler);
        }
        $addHandler($get('criteria',this.get_element()),'keypress',this._criteriaChangeDelegate);

        if (this._cancelClickDelegate === null) {
            this._cancelClickDelegate = Function.createDelegate(this, this._cancelClickHandler);
        }
        $addHandler($get('BtnCancel',this.get_element()),'click',this._cancelClickDelegate);
        
        if (this._saveCallbackSuccessDelegate === null) {
            this._saveCallbackSuccessDelegate = Function.createDelegate(this, this._saveCallbackSuccessHandler);
        }
        
        if (this._saveCallbackFailureDelegate === null) {
            this._saveCallbackFailureDelegate = Function.createDelegate(this, this._saveCallbackFailureHandler);
        }
        
        if (this._LocationsDelegate === null) {
            this._LocationsDelegate = Function.createDelegate(this, this._LocationsHandler);
        }
        
         if (this._EnterKeyPressedDelegate === null) {
            this._EnterKeyPressedDelegate = Function.createDelegate(this, this._saveClickHandler);
        }
        
        //Handle Enter Key
        this.add_enterPressed(this._EnterKeyPressedDelegate);
        
        this._initValues();
    },
    _initValues: function()
    {
        $get("criteria",this.get_element()).value = this.get_city();
        $get('tempType',this.get_element()).value = this.get_tempType();
    },
    updated: function()
    {
        Insp.UI.TelusWeatherEdit.callBaseMethod(this,'updated');
    },
    _saveClickHandler: function(eventObj)
    {
        var loc = $get("criteria",this.get_element()).value;
        var tempType = $get('tempType',this.get_element()).value;
        if(tempType.length > 0)this.set_tempType(tempType);
        
        if(loc.trim().length == 0)
        {
            this._displayError($get("errorMessage",this.get_element()),this._errorMessage2);
            this._wError = true;
            return;
        }
        if(!this._multipleLoc || this._criteriaChanged)
        {
            this._criteriaChanged = false;
            Portal.ClientTelusWeatherSvc.GetLocations(loc,this._LocationsDelegate);
        }
        else
        {
            var oList = $get('selLocation',this.get_element());
            var city = oList.options[oList.selectedIndex].text.split(',');
            this.set_city(city[0]);
            this.set_cityCode(oList.value);
            this._SaveCall();
        }
    },
    _cancelClickHandler: function(eventObj)
    {
        this.get_module().set_showEditPart(false);
        this._initValues();
        this._multipleLoc = false;
        this._criteriaChanged = false;
        
        //Clear Error Message
        var obj = $get("errorMessage",this.get_element());
        obj.innerHTML = "";
        obj.style.display = "none";
        
        //Clear Multiple Results
        var mDiv = $get("ResultsPanel",this.get_element());
        if(mDiv)mDiv.innerHTML = "";
    },
    _criteriaChangeHandler: function(eventObj)
    {
        this._criteriaChanged = true;
    },
    _saveCallbackSuccessHandler: function(res)
    {
        if(res == "success")
        {
            //Clear Multiple Results
            var mDiv = $get("ResultsPanel",this.get_element());
            if(mDiv)mDiv.innerHTML = "";
            this._criteriaChanged = false;
            
            if(this.get_module())this.get_module().refreshModule();
        }
    },
    _saveCallbackFailureHandler: function()
    {
    },
    _cleanList: function(oList)
    {
        for (ic = oList.length - 1; ic>=0; ic--) 
        {
            oList.remove(ic);
        }
    },
    _LocationsHandler: function(result)
    {
        if(result != null)
        {
            if(result.length > 1)
            {
                if(this._wError) this._wError = false;
                var rDiv = $get('ResultsPanel',this.get_element());
                rDiv.innerHTML = "<span style='padding:5px 0px 5px 0px;position:relative'>"+strTelusWeather_MulMessage1+"</span><br><span style='position:relative'>"+strTelusWeather_MulMessage2+"</span><br><select id='selLocation' style='width:255px'/>";
                var sel = $get('selLocation',this.get_element());
                this._cleanList(sel);
                for(var cz =0;cz<result.length;cz++)
                {
                    var oOPT = document.createElement('OPTION');;
                    oOPT.value = result[cz].LocationCode;
                    oOPT.innerHTML = result[cz].Location;
                    sel.appendChild(oOPT);
                }
                this._multipleLoc = true;
            }
            else if(result.length == 1)
            {
                    var loc = result[0].Location.split(',');
                    this.set_city(loc[0]);
                    this.set_cityCode(result[0].LocationCode);
                    this._multipleLoc = false;
                    if(this._wError) this._wError = false;
                    this._SaveCall();
            }
            else
            {
                this._displayError($get("errorMessage",this.get_element()),this._errorMessage1);
            }
            //Reset the flag
            this._criteriaChanged = false;
         }
         else
         {
             //Error
             this._displayError($get("errorMessage",this.get_element()),this._errorMessage1);
             this._wError = true;
         }
    },
    _SaveCall: function()
    {
        if(!this._wError)
        {
            Portal.ClientTelusWeatherSvc.UpdateChanges(this.get_city(),this.get_cityCode(),this.get_tempType(),this.get_moduleClientID(),this._saveCallbackSuccessDelegate,this._saveCallbackFailureDelegate);
        }
        else
        {
            return false;
        }
    },
    _displayError: function(obj,message)
    {
        obj.innerHTML = message;
        obj.style.display = "block";
    }
}

Insp.UI.TelusWeatherEdit.registerClass('Insp.UI.TelusWeatherEdit',Insp.UI.ModuleEditCtl);


// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


